package model;




import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//在这里完成Blog表需要完成的操作
//增删改查
public class BlogDao {

    //新增博客
    public void add(Blog blog){

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 和数据库建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 SQL
            String sql = "insert into blog values(null, ?, ?, ?, ? )";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setTimestamp(4, blog.getPostTimestamp());
            statement.setInt(3, blog.getUserId());
            // 3. 执行 sql
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection, statement, null);
        }
    }
    //根据博客id来查询指定博客
    public Blog SelectById(int blogId){
        //1.和数据库建立连接
        Connection connection= DBUtil.getConnection();
        //2.构造sql语句
        String sql="select * from blog where blogId=?";
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //3.执行sql语句
        try{
            statement=connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //要用statement类的executeQuery()方法来下达select指令以查询数据库，
            // executeQuery()方法会把数据库响应的查询结果存放在ResultSet类对象中供我们使用。
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    //直接从查询出数据库所有的博客
    public List<Blog> SelectAll(){
        List<Blog> blogs=new ArrayList<>();
        //1.和数据库建立连接
        Connection connection= null;
        //2.构造sql语句

        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //3.执行sql语句
        try{
            connection=DBUtil.getConnection();
            String sql="select * from blog order by postTime desc ";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while(resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content=resultSet.getString("content");
                if(content.length()>=100){
                    content= content.substring(0,100)+"......";
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogs.add(blog);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(connection,statement,resultSet);
        }
        return blogs;
    }
    //删除博客
    public void delete(int blogId){
        //1.和数据库建立连接
        Connection connection= DBUtil.getConnection();
        //2.构造sql语句
        String sql="delect from blog where blogId=? ";
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        //3.执行sql语句
        try{
           statement=connection.prepareStatement(sql);
           statement.setInt(1,blogId);
           statement.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(connection,statement,null);
        }
    }
    //修改博客


    //根据用户名得到文章总数
    public int CountByUserId(int userId){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        int count=0;
        try{
            connection=DBUtil.getConnection();
            String sql="select count(*) as total from blog where userId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet=statement.executeQuery();
            resultSet.next();
            count =resultSet.getInt("total");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return count;
    }

}
